package cn.qn.controller.news;

import cn.qn.base.PageVO;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.news.ImageTextDTO;
import cn.qn.dto.news.ImageTextQueryDTO;
import cn.qn.enums.BooleanEnum;
import cn.qn.exceptions.BizException;
import cn.qn.framwork.security.MemberAccountUtils;
import cn.qn.moudle.news.ImageText;
import cn.qn.service.news.ImageTextService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Bryant
 * @date: 2019/5/29
 * @description:
 */
@RestController
@RequestMapping("/image/text")
public class ImageTextController {

    @Reference(check = false, timeout = 30000)
    private ImageTextService imageTextService;

    @RequestMapping(value = "/list", name = "图文列表接口", method = RequestMethod.GET)
    public ResponseDTO list(ImageTextQueryDTO query) {
        PageVO<ImageTextDTO> imageTexts = imageTextService.pageQueryImageTexts(query);
        return ResponseDTO.success(imageTexts);
    }

    @RequestMapping(value = "/detail/{id}", name = "图文详情接口", method = RequestMethod.GET)
    public ResponseDTO detail(@PathVariable(value = "id") Integer id) {
        ImageTextDTO imageText = imageTextService.queryDetailById(id);
        return ResponseDTO.success(imageText);
    }

    @RequestMapping(value = "/add", name = "发布图文接口", method = RequestMethod.POST)
    public ResponseDTO add(@RequestBody ImageTextDTO dto) {
        try {
            dto.setCreateBy(MemberAccountUtils.getCurrentMember().getUsername());
            imageTextService.addImageText(dto);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/edit", name = "修改图文信息接口", method = RequestMethod.POST)
    public ResponseDTO edit(@RequestBody ImageTextDTO dto) {
        try {
            dto.setUpdateBy(MemberAccountUtils.getCurrentMember().getUsername());
            imageTextService.editImageText(dto);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/edit/status", name = "修改图文状态接口", method = RequestMethod.POST)
    public ResponseDTO editStatus(@RequestBody ImageTextDTO dto) {
        try {
            dto.setUpdateBy(MemberAccountUtils.getCurrentMember().getUsername());
            imageTextService.editStatus(dto);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/top/status/{id}", name = "置顶新闻", method = RequestMethod.GET)
    public ResponseDTO topStatus(@PathVariable Integer id) {
        try {
            ImageTextDTO dto = imageTextService.queryDetailById(id);
            if(null == dto){
                return ResponseDTO.error("新闻不存在!");
            }
            if(!dto.getStatus().equals(BooleanEnum.TRUE.getStringCode())){
                return ResponseDTO.error("该新闻为下架状态,请先上架该新闻!");
            }
            ImageText imageText = imageTextService.topStatus(id,MemberAccountUtils.getCurrentMember().getUsername());
            if(null != imageText){
                return ResponseDTO.success(imageText,"置顶成功");
            }
            return ResponseDTO.error("操作失败!");
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/delete/{id}", name = "删除图文信息接口", method = RequestMethod.POST)
    public ResponseDTO deleteRecruit(@PathVariable(value = "id") Integer id) {
        try {
            imageTextService.deleteImageText(id);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }
}
